<?php
class ControllerSaleVoucherTheme extends AdminController {

  	public function index() {

		M('sale/voucher_theme');

    	$this->getList();
  	}

  	public function insert() {

		M('sale/voucher_theme');

		if ($this->request->isPost() && $this->validateForm()) {
      		$this->model_sale_voucher_theme->addVoucherTheme($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
      		$this->redirect(UA('sale/voucher_theme'));
		}

    	$this->getForm();
  	}

  	public function update() {

		M('sale/voucher_theme');

    	if ($this->request->isPost() && $this->validateForm()) {
	  		$this->model_sale_voucher_theme->editVoucherTheme($this->request->get['voucher_theme_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
      		$this->redirect(UA('sale/voucher_theme'));
    	}

    	$this->getForm();
  	}

  	public function delete() {

		M('sale/voucher_theme');

    	if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $voucher_theme_id) {
				$this->model_sale_voucher_theme->deleteVoucherTheme($voucher_theme_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
      		$this->redirect(UA('sale/voucher_theme'));
   		}

    	$this->getList();
  	}

  	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$this->data['voucher_themes'] = array();

		$voucher_theme_total = $this->model_sale_voucher_theme->getTotalVoucherThemes();

		$results = $this->model_sale_voucher_theme->getVoucherThemes($qf);

    	foreach ($results as $result) {
			$action = array();

			$action[] = array(
				'text' => $this->_['text_edit'],
				'href' => UA('sale/voucher_theme/update', 'voucher_theme_id=' . $result['voucher_theme_id'])
			);

			$this->data['voucher_themes'][] = array(
				'voucher_theme_id' => $result['voucher_theme_id'],
				'name'             => $result['name'],
				'action'           => $action
			);
		}

		$this->data['pagination'] = $this->pagination($voucher_theme_total)->render();

		$this->display('sale/voucher_theme_list.tpl');
  	}

  	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);

		if (isset($this->request->get['voucher_theme_id'])) {
			$this->data['action'] = UA('sale/voucher_theme/update', 'voucher_theme_id=' . $this->request->get['voucher_theme_id']);
		} else {
			$this->data['action'] = UA('sale/voucher_theme/insert');
		}

		if (isset($this->request->get['voucher_theme_id']) && !$this->request->isPost()) {
      		$voucher_theme_info = $this->model_sale_voucher_theme->getVoucherTheme($this->request->get['voucher_theme_id']);
    	}

		$this->data['token'] = $this->session->data['token'];

		$this->data['languages'] = C('cache_language');

		if (isset($this->request->post['voucher_theme_description'])) {
			$this->data['voucher_theme_description'] = $this->request->post['voucher_theme_description'];
		} elseif (isset($this->request->get['voucher_theme_id'])) {
			$this->data['voucher_theme_description'] = $this->model_sale_voucher_theme->getVoucherThemeDescriptions($this->request->get['voucher_theme_id']);
		} else {
			$this->data['voucher_theme_description'] = array();
		}

		if (isset($this->request->post['image'])) {
			$this->data['image'] = $this->request->post['image'];
		} elseif (!empty($voucher_theme_info)) {
			$this->data['image'] = $voucher_theme_info['image'];
		} else {
			$this->data['image'] = '';
		}

		$this->data['thumb'] = thumb($voucher_theme_info['image']);
		$this->data['no_image'] = thumb('no_image.jpg');

		$this->display('sale/voucher_theme_form.tpl');
  	}

	protected function validateForm() {
		$pass = true;
    	foreach ($this->request->post['voucher_theme_description'] as $language_id => $value) {
      		if (!range_length($value['name'], 3, 32)) {
        		$this->setMessage('error_name_' . $language_id, $this->_['error_name']);
				$pass = false;
      		}
    	}

		if (!$this->request->post['image']) {
			$this->setMessage('error_image', $this->_['error_image']);
			$pass = false;
		}
		return $pass;
  	}

  	protected function validateDelete() {

		M('sale/voucher');

		foreach ($this->request->post['selected'] as $voucher_theme_id) {
			$voucher_total = $this->model_sale_voucher->getTotalVouchersByVoucherThemeId($voucher_theme_id);

			if ($voucher_total) {
	  			$this->setMessage('error_warning', sprintf($this->_['error_voucher'], $voucher_total));
				return false;
			}
	  	}

  		return true;
  	}
}
?>